<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 银行卡
 */
declare (strict_types=1);

namespace app\api\controller;

use app\api\base\controller\Base;
use app\api\validate\BankCardValidate;

class Bankcard extends Base
{
    /**
     * 详情
     */
    public function getDetail()
    {
        $bandCardInfo = \app\extend\model\BankCard::where([
            ['uid', '=', self::$UserInfo['id']]
        ])->find();

        return json(_S('获取成功', $bandCardInfo));
    }

    /**
     * 绑卡
     */
    public function bindCard()
    {
        if (request()->isPost()) {
            $validate = new BankCardValidate();
            $ret = $validate->scene('bindCard')->check(input());
            if (!$ret) {
                return json(_E($validate->getError()));
            }

            $username = input('username', '');
            $bandName = input('bank_name', '');
            $bandBranchName = input('bank_branch_name', '');
            $number = input('number', '');

            $bankCardInfo = \app\extend\model\BankCard::where([
                ['uid', '=', self::$UserInfo['id']]
            ])->field('id')->find();
            if (!empty($bankCardInfo)) {
                return json(_E('已经绑定过银行卡'));
            }

            $result = \app\extend\model\BankCard::insert([
                'uid' => self::$UserInfo['id'],
                'username' => $username,
                'number' => $number,
                'bank_name' => $bandName,
                'bank_branch_name' => $bandBranchName,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);
            if(!$result){
                return json(_E('绑定失败'));
            } else {
                return json(_S('绑定成功'));
            }
        }
    }
}
